#!/bin/bash
dt='{{ execution_date | cst_ds }}'
name='网点建包率统计临时导数'
# 在退出时清理文件
trap "rm -f /tmp/${name}_${dt}.zip /tmp/${name}_${dt}.csv && echo 'removed tmp files'" EXIT
# 创建txt文件
touch /tmp/${name}_${dt}.txt
#将中文字段名输入文件顶部
#将查询结果追加到文件中
{
  echo -e "日期\t所属代理区code\t所属代理区name\t所属大区code\t所属大区name\t所属加盟商code\t所属加盟商name\t网点编号\t网点\t订单来源编码\t订单来源\t出港发件总票数\t中心到件前实建包票数\t中心到件前实建包率\t循环袋建包票数\t小件I细长件总票数\t循环袋小件细长件建包票数\t需建包票数\t循环袋已建包票数\t应建包票数\t循环袋实建包票数\t应建包(≤3.2kg)\t实建包(≤3.2kg)\t建包率(≤3.2kg)\t是否集散">/tmp/${name}_${dt}.txt;
  spark-sql --driver-memory 8G --num-executors 6  --executor-memory 4g --executor-cores 4  --conf spark.executor.memoryOverhead=4g --conf spark.driver.maxResultSize=3g -S -e "
    SELECT
    date_time
    ,proxyarea_code
    ,proxyarea_name
    ,region_code
    ,region_name
    ,franchisees_code
    ,franchisees_name
    ,site_code
    ,site_name
    ,order_source_code
    ,order_source_name
    ,out_send_cnt
    ,center_bag_cnt
    ,cast(center_bag_cnt/need_bagging_qty32 as decimal(10,4)) as center_bag_befor_rt
    ,repeat_bag_cnt
    ,small_cnt
    ,repeat_small_bag_cnt
    ,need_bagging_no_qty
    ,repeat_need_bag_cnt
    ,need_bagging_qty
    ,repeat_real_bag_cnt
    ,need_bagging_qty32
    ,need_real_baging_qty32
    ,cast(need_real_baging_qty32/need_bagging_qty32 as decimal(10,4)) as bag_rt
    ,case when is_cs=1 then '是' else '否'	END AS is_cs
    FROM jms_dm.dm_network_bagging_rate_dt
    WHERE dt = '{{ execution_date | cst_ds }}';
" >>/tmp/${name}_${dt}.txt
}
# 替换分隔符，并输出csv
cat /tmp/${name}_${dt}.txt | tr -s '\t' ',' > /tmp/${name}_${dt}.csv
# 压缩并删除 csv 文件
zip -jrm /tmp/${name}_${dt}.zip /tmp/${name}_${dt}.csv
# 上传文件
/usr/local/python3/bin/python3  /usr/local/airflow/dags/utils/operators/upload_oss_operation.py ${name}/${name}_${dt}.zip /tmp/${name}_${dt}.zip
# zip 文件将在进程退出后被自动清理

#声明文件大小
data_size=$(ls -lh /tmp/${name}_${dt}.zip | awk '{print $5}')
#打印文件信息
Message="${name}_${dt}.zip 上传成功 大小: $data_size"
#配置钉钉告警
curl 'https://oapi.dingtalk.com/robot/send?access_token=816a8f35fd84432ffa6bff9d7714abc322628bb4a8ded86908550d2335aba613' \
   -H 'Content-Type: application/json' \
   -d "
  {\"msgtype\": \"text\",
    \"text\": {
        \"content\": \"监控内容:$Message\"
     }
  }" || true
rm -f  /tmp/${name}_${dt}.txt
